//
//  THJSubmitOrderVC.m
//  THJXHW
//
//  Created by 河南德艺 on 2018/8/14.
//  Copyright © 2018年 河南德艺. All rights reserved.
//

#import "THJSubmitOrderVC.h"
#import "THJSubmitOrderCell.h"
#import "THJSubmitCountCell.h"
#import "THJSubmitOtherCell.h"
#import "THJSubmitInfoCell.h"
#import "THJSubmitRemarkCell.h"
#import "THJPayStyleVIew.h"
#import "THJGuigeView.h"
#import "THJTransportView.h"
#import "THJBaodaoView.h"
#import "THJHanshuiView.h"
#import "THJContactInfoView.h"
#import "THJTradeContractVC2.h"
#import "THJTradeContractVC.h"
#import "THJMyOrderVC.h"


@interface THJSubmitOrderVC ()<UITableViewDelegate,UITableViewDataSource,THJPayStyleVIewDelegate,THJGuigeViewDelegate,THJTransportViewDelegate,THJBaodaoViewDelegate,THJHanshuiViewDelegate,THJContactInfoViewDelegate,THJSubmitCountCellDelegate>
{
    UITableView * _tableview;
    NSArray * _titleArr;
    NSArray * _tempArr;
//    NSMutableDictionary * _infoDic;
    NSString * _name;
    NSString * _phone;
    NSString * _address;
    THJSubmitCountCell * _countCell;
    THJSubmitRemarkCell * _remarkCell;
    
  
    NSString * type;
    NSString * mobile;
    NSString * buyer_address;
    NSString * buyer_mobile;
    NSString * note;
    NSString * order_type;
    NSString * guiGe;
    NSString * yunshu;
    NSString * baozhuang;
    NSString * hanshui;
    NSString * buyer_name;
    NSString * buyer_phone;
    NSString * number;
    NSString * amount;
}
@property (weak, nonatomic) IBOutlet UILabel *totalLab;

@property(strong,nonatomic)NSMutableArray * contentArr;
@property(strong,nonatomic)NSMutableDictionary * infoDic;
@end

@implementation THJSubmitOrderVC
-(NSMutableArray*)contentArr{
    if(!_contentArr){
        _contentArr = [[NSMutableArray alloc] init];
        [_contentArr addObjectsFromArray:_tempArr];
    }
    return _contentArr;
}
-(NSMutableDictionary*)infoDic{
    if(!_infoDic){
//        _infoDic = [[NSMutableDictionary dictionaryWithObjectsAndKeys:@"请选择",@"name",@"请选择":@"" nil];
       
    }
    return _infoDic;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    _titleArr = @[@"付款方式",@"规格",@"运输",@"包装",@"是否含税",@"收货地址"];
    _tempArr = @[@"请选择",@"请选择",@"请选择",@"请选择",@"请选择"];
    self.view.backgroundColor = SingleColor(whiteColor);
    if(KIsiPhoneX){
        [self setXNavBarWithTitle:@"确认订单"];
    }else{
        [self setNavBarWithTitle:@"确认订单"];
    }
    float  amount = [self.amount floatValue];
    float singprice = [self.price floatValue];
    self.totalLab.text = [NSString stringWithFormat:@"¥%.2f", amount * singprice];
    _tableview = [self setTableviewWithFrame:CGRectMake(0, NavbarHeight, ScreenWidth, ScreenHeight-NavbarHeight-46) RegisterNibNameArr:@[@"THJSubmitOrderCell",@"THJSubmitCountCell",@"THJSubmitOtherCell",@"THJSubmitInfoCell",@"THJSubmitRemarkCell"] ReuseIdentifierArr:@[@"order",@"count",@"other",@"info",@"remark"] RowHeight:0 BgColor:SingleColor(whiteColor) IsLoadMore:NO];

}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        return 115;
    }else if(indexPath.row == 7){
        return 95;
    }else{
        return 50;
    }
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return 9;
}
-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if(indexPath.row == 0){
        THJSubmitOrderCell * cell = [tableView dequeueReusableCellWithIdentifier:@"order" forIndexPath:indexPath];
        cell.imgV.image = self.img;
        cell.titleLab.text = self.pTitle;
        cell.priceLab.text = self.price;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if(indexPath.row == 1){
        THJSubmitCountCell * cell = [tableView dequeueReusableCellWithIdentifier:@"count" forIndexPath:indexPath];
        cell.delegate = self;
        _countCell = cell;
        cell.countTf.text = self.amount;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        return cell;
    }else if(indexPath.row == 7){
        THJSubmitInfoCell * cell = [tableView dequeueReusableCellWithIdentifier:@"info" forIndexPath:indexPath];
        if(_phone){
            cell.phoneLab.text = _phone;
        }else{
            cell.phoneLab.text = @"点击填写";
        }
        cell.nameLab.text = _name;
        cell.addressLab.text = _address;
        return cell;
    }else if(indexPath.row == 8){
        THJSubmitRemarkCell * cell = [tableView dequeueReusableCellWithIdentifier:@"remark" forIndexPath:indexPath];
        _remarkCell = cell;
        return cell;
    }else{
        THJSubmitOtherCell * cell = [tableView dequeueReusableCellWithIdentifier:@"other" forIndexPath:indexPath];
        cell.titleLab.text = _titleArr[indexPath.row - 2];
        cell.contentLab.text = self.contentArr[indexPath.row - 2];
        return cell;
    }
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if(indexPath.row == 2){
       THJPayStyleVIew * view =  [THJPayStyleVIew instancView];
        view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        view.delegate = self;
        view.backgroundColor = COLOR(0, 0, 0, 0.5);
        [self.view addSubview:view];
    }else if(indexPath.row == 3){
        THJGuigeView * view =  [THJGuigeView instancView];
         view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        view.delegate = self;
        view.backgroundColor = COLOR(0, 0, 0, 0.5);
        [self.view addSubview:view];
    }else if(indexPath.row == 4){
        THJTransportView* view =  [THJTransportView instancView];
         view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        view.delegate = self;
        view.backgroundColor = COLOR(0, 0, 0, 0.5);
        [self.view addSubview:view];
    }else if(indexPath.row == 5){
        THJBaodaoView* view =  [THJBaodaoView instancView];
         view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        view.delegate = self;
        view.backgroundColor = COLOR(0, 0, 0, 0.5);
        [self.view addSubview:view];
    }else if(indexPath.row == 6){
        THJHanshuiView* view =  [THJHanshuiView instancView];
         view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        view.delegate = self;
        view.backgroundColor = COLOR(0, 0, 0, 0.5);
        [self.view addSubview:view];
    }else if(indexPath.row == 7){
        THJContactInfoView* view =  [[THJContactInfoView alloc] init];
//        view.nameTf.text = _name;
//        view.phoneTf.text = _phone;
//        view.addressTf.text = _address;
        view.frame = CGRectMake(0, 0, ScreenWidth, ScreenHeight);
        view.delegate = self;
        view.backgroundColor = COLOR(0, 0, 0, 0.5);
        [self.view addSubview:view];
        [self getAddressDataWithView:view];
    }
}
-(void)getAddressDataWithView:(THJContactInfoView*)view{
    
    NSString * mobile = [RSA encryptString:[THJTools getMobile] publicKey:RSA_PubKey];
    NSLog(@"mobile-%@",mobile);
    [THJNetWork postNetDataFromUrl:@"http://p.thjb.net/thjblink/appdata.php?action=shipping_address" parameters:@{@"type":@"ios",@"mobile":mobile} callback:^(NSDictionary *  receiveData, NSError *error) {
        NSLog(@"位置信息--%@",receiveData);
        if([receiveData[@"status"] isEqual:@"success"]){
            NSLog(@"HJHJH-%@-%@-%@",view.nameTf.text,view.phoneTf.text,view.addressTf.text);
            view.nameTf.text = [RSA decryptString:receiveData[@"data"][@"name"] publicKey:RSA_PubKey];
            view.phoneTf.text = [RSA decryptString:receiveData[@"data"][@"mobile"] publicKey:RSA_PubKey];
            view.addressTf.text = [RSA decryptString:receiveData[@"data"][@"address"] publicKey:RSA_PubKey];
            //            NSLog(@"JHKJH-%@-%@-%@",[RSA decryptString:receiveData[@"data"][@"name"] publicKey:RSA_PubKey],[RSA decryptString:receiveData[@"data"][@"mobile"] publicKey:RSA_PubKey],[RSA decryptString:receiveData[@"data"][@"address"] publicKey:RSA_PubKey]);
            NSLog(@"JKJKJK-%@-%@-%@",view.nameTf.text,view.phoneTf.text,view.addressTf.text);
        }else{
//            [self HUDShowMsg:@"获取信息失败" AfterDelayDimiss:1];
        }
    }];
}
//获取吨数
-(void)getTunCount:(NSString *)tuncount{
    float tun = [tuncount floatValue];
    self.amount = tuncount;
    float price = [self.price floatValue];
    NSLog(@"tun-%lf-price-%lf",tun,price);
    amount = [NSString stringWithFormat:@"%.2f",tun * price];
    self.totalLab.text = [NSString stringWithFormat:@"¥%.2f",tun * price];
}
//付款方式
-(void)payStyleSelect:(NSString *)style{
    NSLog(@"fukuanfangshi-%@",style);
    if([style isEqual:@"全款冻结,发货全款解付"]){
        order_type = @"A";
        
    }else if([style isEqual:@"全款冻结,装车分批解付"]){
        order_type = @"B";
       
    }else if([style isEqual:@"全款冻结,货到分批解付"]){
        order_type = @"C";
   
    }else{
        order_type = @"D";
    }
    [self.contentArr replaceObjectAtIndex:0 withObject:style];
    [_tableview reloadData];
}
//规格
-(void)guiguiSelect:(NSString *)guige{
    NSLog(@"guige-%@",guige);
    if([guige isEqual:@"自然块"]){
        guiGe = @"1";
    }else if([guige isEqual:@"标准块"]){
        guiGe = @"2";
    }else if([guige isEqual:@"10-50mm"]){
        guiGe = @"3";
    }else if([guige isEqual:@"10-70mm"]){
        guiGe = @"4";
    }else{
        guiGe = @"5";
    }
    [self.contentArr replaceObjectAtIndex:1 withObject:guige];
    [_tableview reloadData];
}
//运输
-(void)transportStyleSelect:(NSString *)style{
    NSLog(@"yunshu-%@",style);
    if([style isEqual:@"包到"]){
        yunshu = @"1";
    }else{
        yunshu = @"2";
    }
    [self.contentArr replaceObjectAtIndex:2 withObject:style];
    [_tableview reloadData];
}
//包装
-(void)baozhuangSelect:(NSString *)style{
    NSLog(@"baozhuang-%@",style);
    if([style isEqual:@"吨小包"]){
        baozhuang = @"1";
    }else if([style isEqual:@"小袋"]){
        baozhuang = @"2";
    }else{
        baozhuang = @"3";
    }
    [self.contentArr replaceObjectAtIndex:3 withObject:style];
    [_tableview reloadData];
}
//含税
-(void)hanshuiSelect:(NSString *)style{
    NSLog(@"hanshui-%@",style);
    if([style isEqual:@"含税"]){
        hanshui = @"1";
    }else{
        hanshui = @"2";
    }
    [self.contentArr replaceObjectAtIndex:4 withObject:style];
    [_tableview reloadData];
}
//收货地址
-(void)name:(NSString *)name phone:(NSString *)phone address:(NSString *)address{
    NSLog(@"h-%@-h-%@-h-%@",name,phone,address);
    _name = name;
    _phone = phone;
    _address = address;
    [_tableview reloadData];
}
- (IBAction)submitOrder:(id)sender {
//    [self.navigationController pushViewController:[THJTradeContractVC new] animated:YES];
    NSLog(@"%@-%@",_countCell.countTf.text,_remarkCell.remarkTf.text);
    NSString * tunCount = _countCell.countTf.text;
    if(!tunCount || [tunCount floatValue] == 0){
        [self HUDShowMsg:@"请填写吨数" AfterDelayDimiss:1];
        return;
    }else if([self.contentArr containsObject:@"请选择"]){
        [self HUDShowMsg:@"请完善信息后再提交" AfterDelayDimiss:1];
        return;
    }else if(!_name || !_phone || !_address){
        [self HUDShowMsg:@"请完善收货地址或者联系人姓名电话" AfterDelayDimiss:1];
        return;
    }
    [self creatAlertController_sheet];
    
}
-(void)creatAlertController_sheet {
    
    UIAlertController *actionSheet = [UIAlertController alertControllerWithTitle:@"提示" message:@"确定订购吗" preferredStyle:UIAlertControllerStyleAlert];
    
    UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了按钮1，进入按钮1的事件");
    }];
    UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleCancel handler:^(UIAlertAction * _Nonnull action) {
        NSLog(@"点击了确定");
        [self postInfo];
    }];
   
    [actionSheet addAction:action1];
    [actionSheet addAction:action2];
    
    
    [self presentViewController:actionSheet animated:YES completion:nil];
}
-(void)postInfo{
    NSString * totoalPrice = [self.totalLab.text stringByReplacingOccurrencesOfString:@"¥" withString:@""];
    NSDictionary * paraDic = @{
                               @"itemid":[self encrypt:self.itemid],
                               @"type":@"ios",
                               @"mobile":[self encrypt:[THJTools getMobile]],
                               @"buyer_address":[self encrypt:_address],
                               @"buyer_mobile":[self encrypt:_phone],
                               @"note":_remarkCell.remarkTf.text,
                               @"order_type":[self encrypt:order_type],
                               @"guige":[self encrypt:guiGe],
                               @"yunshu":[self encrypt:yunshu],
                               @"baozhuang":[self encrypt:baozhuang],
                               @"hanshui":[self encrypt:hanshui],
                               @"buyer_name":[self encrypt:_name],
                               @"number":[self encrypt:_countCell.countTf.text],
                               @"amount":[self encrypt:totoalPrice]
                               };
    NSLog(@"number:%@-self.totalLab.text:%@",_countCell.countTf.text,totoalPrice);
   
    
    [self SVHUDShow];
    
    [THJNetWork postNetDataFromUrl:@"http://p.thjb.net/thjblink/appdata.php?action=insert_mall_order" parameters:paraDic callback:^(NSDictionary *  receiveData, NSError *error) {
        NSLog(@"下订单--%@",receiveData);
        [self SVHUDHiden];
        if([receiveData[@"status"] isEqual:@"success"]){
//            THJTradeContractVC2 * vc = [THJTradeContractVC2 new];
//            vc.orderId = [RSA decryptString:receiveData[@"data"][@"id"] publicKey:RSA_PubKey];
            THJMyOrderVC * vc = [THJMyOrderVC new];
//            NSLog(@"orderid-----%@",vc.orderId);
            [self.navigationController pushViewController:vc animated:YES];
        }else{
            [self HUDShowMsg:@"提交失败" AfterDelayDimiss:1];
        }
    }];
}
-(NSString*)encrypt:(NSString*)string{
    return  [RSA encryptString:string publicKey:RSA_PubKey];
}
@end
